package com.bwf.lgqingdan.gui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.entity.ResponseHotTags;
import com.bwf.lgqingdan.gui.adapter.HotTagsAdapter;
import com.bwf.lgqingdan.gui.view.NestingGridView;
import com.bwf.lgqingdan.mvp.view.HotTagsView;
import com.bwf.lgqingdan.utils.Apis;
import com.bwf.lgqingdan.utils.IntentUtils;
import com.bwf.lgqingdan.utils.http.lib.HttpUtils;
import com.bwf.lgqingdan.utils.http.lib.Request;
import com.bwf.lgqingdan.utils.http.qingdan.HttpClient;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LG on 2016/11/18.
 * Tips:热门标签 界面
 */

public class HotTagsActivity extends ActivityBase implements HotTagsView, AdapterView.OnItemClickListener {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.textView_title_right)
    TextView textViewTitleRight;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.refreshLayout)
    MaterialRefreshLayout refreshLayout;
    @BindView(R.id.root)
    LinearLayout root;

    @Override
    public int getContentViewResID() {
        return R.layout.activity_hot_tags;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        root.setPadding(0, getStatusHeight(), 0, 0);
        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                loadDatas();
            }
        });

    }

    @Override
    protected void initDatas() {
        new Handler(){
            @Override
            public void handleMessage(Message msg) {
                refreshLayout.autoRefresh();
            }
        }.sendEmptyMessageDelayed(0,350);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @OnClick({R.id.back, R.id.textView_title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                clickBack();
                break;
            case R.id.textView_title_right:
                clickAllTags();
                break;
        }
    }

    private void loadDatas() {
        String url = Apis.API_HOT_TAGS;
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();
        HttpClient.excute(builder, new HttpUtils.Callback() {
            @Override
            public void onResponse(String response) {
                ResponseHotTags responseHotTags = JSON.parseObject(response, ResponseHotTags.class);
                if (responseHotTags.getCode() == 0) {
                    showAllHotTags(responseHotTags.getData().getSections());
                } else {
                    showLoadedFailed(responseHotTags.getMessage());
                }
                refreshLayout.finishRefresh();
            }
            @Override
            public void onError() {
                showLoadedFailed("连接服务器失败");
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void clickBack() {
        finish();
    }

    @Override
    public void clickAllTags() {
        IntentUtils.gotoAllTagsActivity(this);
    }

    @Override
    public void clickTagItem(ResponseHotTags.DataBean.SectionsBean.ItemsBean itemsBean) {
        IntentUtils.gotoCategoryThingActivity(this,itemsBean.getTag_id(),itemsBean.getTitle());
    }

    @Override
    public void showAllHotTags(List<ResponseHotTags.DataBean.SectionsBean> sections) {
        container.removeAllViews();//先移除所有子View
        for (int i = 0; i < sections.size(); i++) {
            View view = getLayoutInflater().inflate(R.layout.layout_item_hot_tag_all, null);
            TagViewHolder holder = new TagViewHolder(view);
            HotTagsAdapter hotTagsAdapter = new HotTagsAdapter(this);
            hotTagsAdapter.setDatas(sections.get(i).getItems());
            holder.rgvSectionItems.setAdapter(hotTagsAdapter);
            holder.rgvSectionItems.setOnItemClickListener(this);
            holder.ivSectionTitle.setImageURI(sections.get(i).getFeaturedImageUrl());
            container.addView(view);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showLoadedFailed(String failedMsg) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ResponseHotTags.DataBean.SectionsBean.ItemsBean itemsBean =
                (ResponseHotTags.DataBean.SectionsBean.ItemsBean) parent.getAdapter().getItem(position);
        clickTagItem(itemsBean);
    }

    class TagViewHolder {
        @BindView(R.id.iv_section_title)
        SimpleDraweeView ivSectionTitle;
        @BindView(R.id.tv_section_title)
        TextView tvSectionTitle;
        @BindView(R.id.rgv_section_items)
        NestingGridView rgvSectionItems;

        TagViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
